package pl.softech.gpw.candles.editor.action;

import java.awt.Point;
import java.awt.Rectangle;

import pl.softech.gpw.candles.editor.CandlestickScene;
import pl.softech.gpw.candles.editor.CandlestickWidget;
import pl.softech.gpw.candles.editor.PaletteCandelstickWidgets;
import pl.softech.gpw.candles.editor.dad.CWidgetDropEvent;
import pl.softech.gpw.candles.editor.dad.CWidgetDropListener;
import pl.softech.gpw.model.FIHelper;

public class CWidgetMotionListenerImpl implements CWidgetMoveListener, CWidgetDropListener {

    private final CandlestickScene scene;
    private double point2pixel;
    
    public CWidgetMotionListenerImpl(CandlestickScene scene) {
        this.scene = scene;
        this.point2pixel = PaletteCandelstickWidgets.point2pixel();
    }

    @Override
    public void movePerformed(CWidgetMoveEvent event) {
        
        CandlestickWidget widget = event.getWidget();
        Point[] disp = event.getDisp();
        double len = (disp[1].getY() - disp[0].getY()) * point2pixel;
        FIHelper.add(widget.getCandlestick().instrument(), -len);
    }

    @Override
    public void dropPerformed(CWidgetDropEvent event) {
        
        CandlestickWidget widget = event.getWidget();
        Point loc = widget.getWidget().getLocation();
        Rectangle sbounds = scene.getScene().getBounds();
        Rectangle wbounds = widget.getWidget().getBounds();
        
        double len = (sbounds.getHeight() - loc.y - wbounds.getHeight()) * point2pixel;
        FIHelper.add(widget.getCandlestick().instrument(), len);
    }

}
